﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace McMySkin_Installer
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            txtBox.Text = openFileDialog1.FileName;
        }

        private void btnInstall_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtBox.Text))
            {
                MessageBox.Show("文件没有选定 =w=");
            }
            else
            {

                this.install();
                
            }
        }

        private void install()
        {
            string path = txtBox.Text;

            if (File.Exists(path))
            {
                if (checkBox1.Checked)
                {
                    File.Copy(txtBox.Text, txtBox.Text + ".mmsbak", true);
                }

                Installer installer = null;

                if (checkBox2.Checked)
                {
                    if (radioButton1.Checked)
                    {
                        installer = new Installer(txtBox.Text, InstallTpye.INSTALL_MOJANG);
                    }
                    else if (radioButton2.Checked)
                    {
                        installer = new Installer(txtBox.Text, InstallTpye.INSTALL_SM);
                    }
                    else
                    {
                        installer = new Installer(txtBox.Text, InstallTpye.INSTALL_MMS);
                    }
                }
                else
                {
                    installer = new Installer(txtBox.Text, InstallTpye.INSTALL_MMS);
                }

                new Thread(() => { installer.Run(); }).Start();
                new Thread(() =>
                {   btnInstall.Enabled = false;
                    while (!installer.done)
                    {
                        label3.Text = "安装中... " + progressBar1.Value + "%";
                        progressBar1.Value = (int)(installer.Progress * 100);
                    } MessageBox.Show("安装完成");
                    label3.Text = "未开始安装";
                    btnInstall.Enabled = true;progressBar1.Value = 0;}).Start();
            }
            else
            {
                MessageBox.Show("文件不存在！");
            }

            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "minecraft.jar";
            openFileDialog1.Title = "选择 minecraft.jar";
            openFileDialog1.Filter = "Minecraft Jar 文件(*.jar)|minecraft.jar";
            openFileDialog1.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            new FormLicense().ShowDialog();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.Description = "请选择您的 .minecraft\\bin 文件夹";
            folderBrowserDialog1.ShowDialog();

            string path = folderBrowserDialog1.SelectedPath;

            if (!string.IsNullOrEmpty(path))
            {

                if (File.Exists(path + "\\minecraft.jar.mmsbak"))
                {
                    File.Delete(path + "\\minecraft.jar");
                    File.Copy(path + "\\minecraft.jar.mmsbak", path + "\\minecraft.jar");
                    File.Delete(path + "\\minecraft.jar.mmsbak");
                    MessageBox.Show("还原完成");
                }
                else
                {
                    MessageBox.Show("没有发现备份");
                }
            }
            
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            bool b = checkBox2.Checked;
            radioButton1.Enabled = b;
            radioButton2.Enabled = b;
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}
